package com.fenbi.tutor.live.module.large.large.chat.lithoview

import android.graphics.Color
import android.util.Log
import com.facebook.litho.Column
import com.facebook.litho.Component
import com.facebook.litho.ComponentContext
import com.facebook.litho.Handle
import com.facebook.litho.annotations.Event
import com.facebook.litho.annotations.FromEvent
import com.facebook.litho.annotations.FromTrigger
import com.facebook.litho.annotations.OnEvent
import com.facebook.litho.annotations.OnTrigger
import com.facebook.litho.annotations.Prop
import com.facebook.litho.annotations.State
import com.facebook.litho.sections.Children
import com.facebook.litho.sections.SectionContext
import com.facebook.litho.sections.annotations.GroupSectionSpec
import com.facebook.litho.sections.annotations.OnCreateChildren
import com.facebook.litho.sections.common.DataDiffSection
import com.facebook.litho.sections.common.RenderEvent
import com.facebook.litho.widget.ComponentRenderInfo
import com.facebook.litho.widget.RenderInfo
import com.facebook.litho.widget.Text
import java.lang.IllegalStateException

/**
 * TODO 简要描述
 * @author wkm @ Zebra Inc.
 * @since 09-19-2020
 */

interface MsgInfo {

    val msg: CharSequence

}

class NormalMsgInfo(override val msg: CharSequence) : MsgInfo {
}

class BanUnbanMsgInfo(override val msg: CharSequence) : MsgInfo {
}

@Event
class OnNewMsgEvent() {
    @JvmField
    var msg: MsgInfo? = null
}

@GroupSectionSpec
object ChatMsgSectionSpec {

    @OnCreateChildren
    fun onCreateChildren(
        c: SectionContext,
        @Prop newMsgHandle: Handle,
        @State msgList: List<MsgInfo>
    ): Children {
        return Children.create()
            .child(
                DataDiffSection.create<MsgInfo>(c)
                    .data(msgList)
                    .renderEventHandler(ChatMsgSection.onRender(c))
            )
            .build()
    }

    @OnEvent(RenderEvent::class)
    fun onRender(c: SectionContext, @FromEvent model: MsgInfo): RenderInfo {
        Log.e("TAG", model.toString())
        return ComponentRenderInfo.create()
            .component(
                when (model) {
                    is NormalMsgInfo -> genNormal(c, model)
                    is BanUnbanMsgInfo -> genBanUnban(c, model)
                    else -> Column.create(c).build()
                }
            )
            .build()
    }


    fun genNormal(c: ComponentContext, msg: MsgInfo): Component {
        if (msg !is NormalMsgInfo) {
            throw IllegalStateException()
        }
        return Text.create(c)
            .textSizeDip(20f)
            .text(msg.msg)
            .backgroundColor(Color.BLUE)
            .build()
    }

    fun genBanUnban(c: ComponentContext, msg: MsgInfo): Component {

        if (msg !is BanUnbanMsgInfo) {
            throw IllegalStateException()
        }
        return Text.create(c)
            .textSizeDip(20f)
            .text(msg.msg)
            .backgroundColor(Color.RED)
            .heightDip(40F)
            .build()
    }

    @OnTrigger(OnNewMsgEvent::class)
    fun onNewMsg(c: SectionContext, @State msgList: List<MsgInfo>, @FromTrigger msg: MsgInfo) {

    }

}